package com.choosefine.base.shixiaobao.common.utils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import Com.FirstSolver.Splash.FaceId_Item;

public class MapUtil {
    /**
     * Bean实体转化成MultiValueMap
     *
     * @param obj
     * @return
     */
    public static MultiValueMap<String, String> beanToMultiValueMap(Object obj) {
        MultiValueMap<String, String> params = null;
        try {
            params = new LinkedMultiValueMap<>();
            Class<?> actualEditable = obj.getClass();
            PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(actualEditable);
            for (PropertyDescriptor targetPd : targetPds) {
                Method readMethod = targetPd.getReadMethod();
                if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) {
                    readMethod.setAccessible(true);
                }
                String name = targetPd.getName();
                Object value = readMethod.invoke(obj);
                if (value != null) {
                    if ("createdAt".equals(name) || "updatedAt".equals(name) || "receiveAt".equals(name) || "deletedAt".equals(name)) {
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        value = simpleDateFormat.format((Date) value);
                    }
                    if (!"class".equals(targetPd.getName()))
                        params.add(targetPd.getName(), value.toString());
                }
            }
        } catch (BeansException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return params;
    }

    // Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean
    public static void transMap2Bean(Map<String, Object> map, Object obj) {

        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                if (map.containsKey(key)) {
                    Object value = map.get(key);
                    // 得到property对应的setter方法  
                    Method setter = property.getWriteMethod();
                    setter.invoke(obj, value);
                }

            }

        } catch (Exception e) {
            System.out.println("transMap2Bean Error " + e);
        }

        return;

    }

    // Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map  
    public static Map<String, Object> transBean2Map(Object obj) {

        if (obj == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor property : propertyDescriptors) {
                String key = property.getName();

                // 过滤class属性  
                if (!key.equals("class")) {
                    // 得到property对应的getter方法  
                    Method getter = property.getReadMethod();
                    Object value = getter.invoke(obj);

                    map.put(key, value);
                }

            }
        } catch (Exception e) {
            System.out.println("transBean2Map Error " + e);
        }

        return map;
    }

    /**
     * 将指定的类型 Iterator 遍历为集合
     *
     * @auth liaozhanggen 2016-11-23 下午1:05:38
     */
    public static List transIterator2Array(Iterator iterator) {
        List list = new ArrayList<>();
        while (iterator.hasNext()) {
            Object element = iterator.next();
            list.add(element);
        }
        return list;
    }

    public Object getObjectFromMap(Map<String, Object> map, String data) {
        Object obj = map.get(data);
        return obj;
    }


    /**
     * 从Map对象中获取list并取得list中Map
     *
     * @param map
     * @param data
     * @return
     */
    public Map<String, Object> getMapFromListMap(List<Map<String, Object>> maps, int index) {
        Map<String, Object> map = (Map<String, Object>) maps.get(index);
        return map;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getListFromMap(Map<String, Object> map, String data) {
        List<Map<String, Object>> array = null;
        array = (List<Map<String, Object>>) map.get(data);
        return array;
    }

    /**
     * 从数据对象Map里获取MapList并获取里面的Map对象
     *
     * @param maps
     * @param index
     * @return
     */
    public Map<String, Object> getObjectMapFromDataMap(List<Map<String, Object>> maps, int index) {
        Map<String, Object> map = (Map<String, Object>) maps.get(index);
        return map;
    }

    public static Map<String, Object> faceItemChangeMap(FaceId_Item[] faceId_items) {
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < faceId_items.length; i++) {
            map.put(faceId_items[i].name, faceId_items[i].value);
        }
        return map;
    }

}
